<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="产品">
          <el-input
            v-model="searchParam.materialCode"
            placeholder="请选择产品"
            clearable
            readonly
            @click="showWorkOrderSelect"
          >
            <template #append>
              <el-button @click="resetMaterial"> 清除 </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <!--<el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >-->
        <el-button type="primary" @click="materialImport">
          <template #icon><i-ep-upload /></template>导入
        </el-button>
        <el-button
          class="ml-3"
          @click="
            useDownload(
              '/mes/mesmaterialpackageconfig/export',
              '包装物料配置表',
              searchParam
            )
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <template v-for="item in tableColumns" :key="item">
          <TableColumn v-if="item.prop && item.isShow" :column="item">
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"></slot>
            </template>
          </TableColumn>
        </template>
        <!-- <el-table-column
          label="物料编号"
          prop="materialCode"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="物料名称"
          prop="materialName"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="产品包装规格（件/箱）"
          prop="productPackagingSpecifications"
          min-width="120"
          show-overflow-tooltip
          header-align="center"
          align="right"
        />
        <el-table-column
          label="器具分类"
          prop="classificationInstruments"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="器具型号"
          prop="instrumentModel"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="包装袋型号"
          prop="packagingBagModel"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="隔板型号"
          prop="partitionModel"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="隔板数量（张/箱）"
          prop="partitionsNumber"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="right"
        />
        <el-table-column
          label="打包带型号"
          prop="packingBeltModel"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="打包带用量（公斤/箱）"
          prop="packingBeltUsage"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="right"
        />
        <el-table-column
          label="打包扣型号"
          prop="packingBuckleModel"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="打包扣用量（个/箱）"
          prop="packagingDeductionQuantity"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="right"
        />
        <el-table-column
          label="拉伸膜型号"
          prop="stretchFilmModel"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="拉伸膜用量（卷/托）"
          prop="stretchFilmDosage"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="right"
        />
        <el-table-column
          label="木块型号"
          prop="woodenBlockModel"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="木块用量（个/箱）"
          prop="woodenBlockQuantity"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="干燥剂型号"
          prop="desiccantModel"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="干燥剂用量（包/箱）"
          prop="desiccantDosage"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="right"
        />
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="创建人"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="更新时间"
          prop="lastUpdateTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="更新人"
          prop="lastUpdatedUserName"
          min-width="100"
          show-overflow-tooltip
        /> -->
        <!--<el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>-->
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mesmaterialpackageconfig-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
    <!-- 导入 -->
    <ImportExcel ref="materialImportRef" />
    <!-- 导入失败错误信息弹窗 -->
    <upload-error-table
      ref="uploadErrorRef"
      :errorDataList="errorDataList"
      :promptDataList="promptDataList"
      :paramList="paramList"
      :listWorkOrder="listWorkOrder"
      @changelist="changelist"
    />
    <!--物料选择-->
    <conten-select
      :visible="selectDialogSELECT.visible"
      @change-visible="changeVisibleSELECT"
      :plantId="searchParam.plantId"
      :brekList="brekList"
    />
    <!-- 列设置 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MESMESMATERIALPACKAGECONFIG",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mesmaterialpackageconfig/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  materialId: "",
  materialCode: "",
};

const deleteParam = {
  api: "/mes/mesmaterialpackageconfig/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mesmaterialpackageconfig/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开包装物料配置表表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改包装物料配置表";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增包装物料配置表";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开包装物料配置表表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "包装物料配置表详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

// 导入数据
const uploadApi = "/mes/mesmaterialpackageconfig/upload";
const materialImportRef = ref();
const uploadErrorRef = ref();
const errorDataList = ref([]);
const promptDataList = ref([]);
const listWorkOrder = ref([]);
const paramList = ref({});
async function materialImportApiFunc(formdata: FormData, config: any) {
  await request.post(uploadApi, formdata, config).then((res: any) => {});
}
const changelist = () => {
  search();
};
const materialImport = () => {
  let newSearchParam = { ...searchParam.value };
  let params = {
    title: "导入包装物料配置",
    tempApi: "/mes/mesmaterialpackageconfig/exportTemp", //模板下载
    importApi: materialImportApiFunc, // 导入
    getTableList, // 成功后回调
    isShowTemp: true, // 是否显示下载模板
    fileTypes: ".xlsx",
    searchParam: newSearchParam,
  };
  materialImportRef.value.acceptParams(params);
};
//产品选择
const showWorkOrderSelect = () => {
  selectDialogSELECT.value.visible = true;
};
//选择物料弹窗
const selectDialogSELECT = ref({
  visible: false,
});
const changeVisibleSELECT = (val: boolean, data: any) => {
  selectDialogSELECT.value.visible = val;
  if (data) {
    searchParam.value.materialId = data.id;
    searchParam.value.materialCode = data.materialCode;
  }
};
// 回显的数据
const brekList = ref([]);
const resetMaterial = () => {
  searchParam.value.materialId = "";
  searchParam.value.materialCode = "";
};

const tableColumns = reactive([
  {
    prop: "materialCode",
    label: "物料编号",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialName",
    label: "物料名称",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "productPackagingSpecifications",
    label: "产品包装规格（件/箱）",
    minWidth: 120,
    isShow: true,
    align: "right",
    headerAlign: "center",
  },
  {
    prop: "classificationInstruments",
    label: "器具分类",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "instrumentModel",
    label: "器具型号",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "packagingBagModel",
    label: "包装袋型号",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "partitionModel",
    label: "隔板型号",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "partitionsNumber",
    label: "隔板数量（张/箱）",
    minWidth: 100,
    isShow: true,
    align: "right",
    headerAlign: "center",
  },
  {
    prop: "packingBeltModel",
    label: "打包带型号",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "packingBeltUsage",
    label: "打包带用量（公斤/箱）",
    minWidth: 100,
    isShow: true,
    align: "right",
    headerAlign: "center",
  },
  {
    prop: "packingBuckleModel",
    label: "打包扣型号",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "packagingDeductionQuantity",
    label: "打包扣用量（个/箱）",
    minWidth: 100,
    isShow: true,
    align: "right",
    headerAlign: "center",
  },
  {
    prop: "stretchFilmModel",
    label: "拉伸膜型号",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "stretchFilmDosage",
    label: "拉伸膜用量（卷/托）",
    minWidth: 100,
    isShow: true,
    align: "right",
    headerAlign: "center",
  },
  {
    prop: "woodenBlockModel",
    label: "木块型号",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "woodenBlockQuantity",
    label: "木块用量（个/箱）",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "desiccantModel",
    label: "干燥剂型号",
    minWidth: 150,
    isShow: true,
    align: "left",
  },
  {
    prop: "desiccantDosage",
    label: "干燥剂用量（包/箱）",
    minWidth: 100,
    isShow: true,
    align: "right",
    headerAlign: "center",
  },
  {
    prop: "createTime",
    label: "创建时间",
    minWidth: 160,
    isShow: true,
    align: "center",
  },
  {
    prop: "createdUserName",
    label: "创建人",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "lastUpdateTime",
    label: "更新时间",
    minWidth: 160,
    isShow: true,
    align: "center",
  },
  {
    prop: "lastUpdatedUserName",
    label: "更新人",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
]);

const searchColumns = reactive([]);
</script>
